#include "addfrienddialog.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QScrollArea>
#include <QPushButton>
#include <QLabel>
#include <QScrollBar>

#include "model/datacenter.h"
#include "debug.h"

using namespace model;

////////////////////////////////////////
/// 表示一个好友搜索的结果
////////////////////////////////////////

FriendResultItem::FriendResultItem(const model::UserInfo &userInfo)
    : userInfo(userInfo)
{
    // 1. 设置基本属性
    this->setFixedHeight(70);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setHorizontalSpacing(0);
    layout->setVerticalSpacing(10);
    layout->setContentsMargins(10, 0, 10, 0);
    this->setLayout(layout);

    // 3. 创建头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(40, 40);
    avatarBtn->setIconSize(QSize(40, 40));
    avatarBtn->setIcon(userInfo.avatar);

    // 4. 创建用户昵称
    QLabel* nameLabel = new QLabel();
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    nameLabel->setStyleSheet("QLabel { font-size: 14px; font-weight: 400; margin-left: 5px; margin-top: 17px;"
                             "background-color: transparent; }");
    nameLabel->setText(userInfo.nickname);

    // 5. 个性签名
    QLabel* descLabel = new QLabel();
    descLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    descLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    descLabel->setStyleSheet("QLabel { font-size: 10px; margin-left: 7px; margin-bottom: 15px; "
                             "color: rgb(153, 153, 153); background-color: transparent; }");
    descLabel->setText(userInfo.description);

    // 6. 添加好友 按钮
    addBtn = new QPushButton();
    addBtn->setFixedSize(62, 25);
    addBtn->setText("添加");
    QString btnStyle = "QPushButton { border: 1px solid rgb(188, 188, 188); background-color: rgb(255, 255, 255); "
                       "color: black; border-radius: 3px; }";
    btnStyle += "QPushButton:hover { background-color: rgb(235, 235, 235); }"
                "QPushButton:pressed { background-color: rgb(215, 215, 215); color: rgb(107, 107, 107); }";
    addBtn->setStyleSheet(btnStyle);

    // 7. 添加到布局管理器
    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1);
    layout->addWidget(descLabel, 1, 1);
    layout->addWidget(addBtn, 0, 2, 2, 1);

    // 8. 连接信号槽
    connect(addBtn, &QPushButton::clicked, this, &FriendResultItem::clickAddBtn);
}

void FriendResultItem::clickAddBtn()
{
    // 1. 发送好友申请
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->addFriendApplyAsync(this->userInfo.userId);

    // 2. 设置按钮为禁用
    addBtn->setEnabled(false);
    addBtn->setText("已申请");
    addBtn->setStyleSheet("QPushButton { border: 1px solid rgb(188, 188, 188); background-color: rgb(235, 235, 235); "
                          "color: rgb(158, 158, 158); border-radius: 3px; }");
}



////////////////////////////////////////
/// 整个搜索好友的窗口
////////////////////////////////////////

AddFriendDialog::AddFriendDialog(QWidget* parent)
    : QDialog(parent)
{
    // 1. 设置基本属性
    this->setFixedSize(350, 500);
    this->setWindowTitle("添加好友");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);


    // 2. 添加布局管理器
    layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(10, 20, 10, 0);
    this->setLayout(layout);

    // 3. 搜索框
    QAction* search = new QAction();
    search->setIcon(QIcon(":/resource/image/friend_inactive.png"));
    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(27);
    searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QString style = "QLineEdit { border:none; border-radius: 5px; font-size: 12px; "
                    "background-color: rgb(226, 226, 226);}";
    searchEdit->setStyleSheet(style);
    searchEdit->setPlaceholderText("微信号/手机号");
    searchEdit->addAction(search, QLineEdit::LeadingPosition);
    layout->addWidget(searchEdit, 0, 0, 1, 8);

    // 4. 搜索按钮
    QPushButton* searchBtn = new QPushButton();
    searchBtn->setFixedSize(27, 27);
    searchBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    searchBtn->setIconSize(QSize(20, 20));
    searchBtn->setIcon(QIcon(":/resource/image/search.png"));
    QString btnStyle = "QPushButton { background-color: rgb(235, 235, 235); border-radius: 5px; border: none; }";
    btnStyle += "QPushButton:hover { background-color: rgb(220, 220, 220); }"
                "QPushButton:pressed { background-color: rgb(200, 200, 200); }";
    searchBtn->setStyleSheet(btnStyle);
    layout->addWidget(searchBtn, 0, 8, 1, 1);

    // 5. 滚动区域
    initResultArea();

    //
#if TEST_UI
    QIcon avatar(":/resource/image/Mitsy.jpg");
    for (int i = 0; i < 20; ++i)
    {
        model::UserInfo* userInfo = new model::UserInfo();
        userInfo->userId = QString::number(1000 + i);
        userInfo->nickname = "野原美伢" + QString::number(i);
        userInfo->description = "购不完的商场";
        userInfo->avatar = avatar;
        this->addResult(*userInfo);
    }
#endif

    // 6. 连接信号槽
    connect(searchBtn, &QPushButton::clicked, this, &AddFriendDialog::clickSearchBtn);
}

void AddFriendDialog::initResultArea()
{
    // 1. 创建滚动区域对象
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    scrollArea->setWidgetResizable(true);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0; } ");
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px;"
                                                   "background-color: rgb(255, 255, 255);} ");
    scrollArea->setStyleSheet("QScrollArea { border: none; }");
    layout->addWidget(scrollArea, 1, 0, 1, 9);

    // 2. 创建 QWidget
    resultContainer = new QWidget();
    resultContainer->setObjectName("resultContainer");
    resultContainer->setStyleSheet("#resultContainer { background-color: rgb(255, 255, 255); }");
    scrollArea->setWidget(resultContainer);

    // 3. 添加元素
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0, 0, 0, 0);
    resultContainer->setLayout(vlayout);
}

void AddFriendDialog::addResult(const model::UserInfo &userInfo)
{
    FriendResultItem* item = new FriendResultItem(userInfo);
    resultContainer->layout()->addWidget(item);
}

void AddFriendDialog::clear()
{
    // 从后往前遍历
    QVBoxLayout* layout = dynamic_cast<QVBoxLayout*>(resultContainer->layout());
    for (int i = layout->count() - 1; i >= 0; --i)
    {
        QLayoutItem* layoutItem = layout->takeAt(i);
        if (layoutItem == nullptr || layoutItem->widget() == nullptr)
            continue;
        delete layoutItem->widget();
    }
}

void AddFriendDialog::setSearchKey(const QString &searcKey)
{
    searchEdit->setText(searcKey);
}

void AddFriendDialog::clickSearchBtn()
{
    // 1. 拿到输入框的内容
    const QString& text = searchEdit->text();
    if (text.isEmpty())
        return;

    // 2. 给服务器发送请求
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::searchUserDone, this, &AddFriendDialog::clickSearchBtnDone,
            Qt::UniqueConnection);
    dataCenter->searchUserAsync(text);
}

void AddFriendDialog::clickSearchBtnDone()
{
    // 1. 拿到 DataCenter 中的搜索结果
    DataCenter* dataCenter = DataCenter::getInstance();
    QList<UserInfo>* searchResult = dataCenter->getSearchUserResult();
    if (searchResult == nullptr)
        return;

    this->clear();
    for (const auto& u : *searchResult)
        this->addResult(u);
}



















